package com.mxpio.erp.technology.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mxpio.erp.common.enums.OrderStatusEnums;
import com.mxpio.erp.common.technology.entity.BomInfoAudit;
import com.mxpio.erp.common.technology.entity.BomInfoLineAudit;
import com.mxpio.erp.common.technology.service.BomInfoAuditService;
import com.mxpio.erp.common.technology.service.BomInfoLineAuditService;
import com.mxpio.erp.common.technology.vo.AuditOpinionVO;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.policy.CrudContext;
import com.mxpioframework.jpa.policy.impl.SmartCrudPolicyAdapter;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

@Tag(name = "BomInfoAuditController", description = "Bom审核接口")
@RestController
@RequestMapping("/erp/tech/bom/audit/")
public class BomInfoAuditController {

    @Autowired
    private BomInfoAuditService bomInfoAuditService;

    @Autowired
    private BomInfoLineAuditService bomInfoLineAuditService;

    @GetMapping("listByItem/{bomId}")
    @Operation(summary = "Bom列表", description = "获取Bom列表（无分页）", method = "GET")
    public Result<BomInfoAudit> listByItem(@Parameter(description="bomId") @PathVariable(name = "bomId", required = true) String bomId){
        BomInfoAudit bomInfo = bomInfoAuditService.listBomAudit(bomId);
        return Result.OK(bomInfo);
    }
    
    @GetMapping("list")
    @Operation(summary = "Bom列表", description = "获取Bom列表（无分页）", method = "GET")
    public Result<List<BomInfoAudit>> list(Criteria criteria) throws UnsupportedEncodingException {
        List<BomInfoAudit> bomInfos = bomInfoAuditService.list(BomInfoAudit.class,criteria);
        return Result.OK(bomInfos);
    }
    @GetMapping("page")
    @Operation(summary = "Bom分页，以物料为基础展示", description = "获取Bom列表（分页）", method = "GET")
    public Result<Page<BomInfoAudit>> page(Criteria criteria, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<BomInfoAudit> bomInfos = bomInfoAuditService.listPage(BomInfoAudit.class,page,criteria);
        return Result.OK(bomInfos);
    }


    @GetMapping("list/{bomId}")
    @Operation(summary = "根据bomId获取Bom", description = "根据bomId获取Bom", method = "GET")
    public Result<BomInfoAudit> getById(@PathVariable(name = "bomId", required = true) String bomId) {
        BomInfoAudit bomInfo = bomInfoAuditService.getById(BomInfoAudit.class, bomId);
        return Result.OK(bomInfo);
    }

    @RequestMapping("save")
    @Operation(summary = "保存Bom", description = "保存Bom")
    public Result<BomInfoAudit> save(@RequestBody BomInfoAudit bomInfo) {
        bomInfoAuditService.save(bomInfo,new SmartCrudPolicyAdapter(){
            @Override
            public boolean beforeInsert(CrudContext context) {
                Object o = context.getEntity();
                if(o instanceof BomInfoLineAudit){
                    ((BomInfoLineAudit) o).setBomId(bomInfo.getBomId());
                }
                return true;
            }
        });
        return Result.OK(bomInfo);
    }

    @DeleteMapping("remove/{bomIds}")
    @Operation(summary = "根据bomIds删除Bom", description = "删除Bom", method = "DELETE")
    @Transactional(readOnly = false)
    public Result<BomInfoAudit> remove(@Parameter(description="bomId，多个id用英文逗号分隔") @PathVariable(name = "bomIds", required = true) String bomIds) {
        String[] bomId = bomIds.split(",");
        for(String key : bomId){
            Criteria c = Criteria.create().addCriterion("bomId", Operator.EQ, key);
            bomInfoLineAuditService.removeBatch(BomInfoLineAudit.class, c);
            bomInfoAuditService.delete(BomInfoAudit.class, key);
        }
        return Result.OK();
    }


    @PutMapping("audit/{bomId}")
    @Operation(summary = "审核Bom", description = "审核Bom", method = "PUT")
    public Result<BomInfoAudit> audit(@Parameter(description="bomId") @PathVariable(name = "bomId", required = true) String bomId,
                                      @RequestBody AuditOpinionVO auditOpinionVO) {
        BomInfoAudit bomInfo = bomInfoAuditService.getById(BomInfoAudit.class,bomId);
        if(bomInfo.getUseType().equals(OrderStatusEnums.BomStatus.AUDITING.getStatusCode())){
            BomInfoAudit audit = bomInfoAuditService.audit(bomInfo,auditOpinionVO);
            return Result.OK(audit);
        }else {
            return Result.error("非审核中状态不可审核");
        }
    }


}
